package com.hopkinscsc.windows;

import java.awt.Graphics;
import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.MediaTracker;

public class Icon extends Element {
	
	private Image image;
	private Component myComponent;

	public Icon(String fileName, int x, int y, Component component)
	{
		super(x, y, 0, 0);
		image = Toolkit.getDefaultToolkit().createImage(fileName);
		myComponent = component;
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(component);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}
		
		if(image.getWidth(null) < 0)
		{
			System.out.print("Error: could not find image: ");
			System.out.print(fileName);
		}
	}
	
	public Icon(String fileName, int x, int y, int width, int height, Component component)
	{
		super(x, y, 0, 0);
		image = Toolkit.getDefaultToolkit().createImage(fileName).getScaledInstance(width, height, Image.SCALE_SMOOTH);
		myComponent = component;
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(component);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}
		
		if(image.getWidth(null) < 0)
		{
			System.out.print("Error: could not find image: ");
			System.out.print(fileName);
		}
	}
	
	public Icon(Image myImage, int x, int y, Component component)
	{
		super(x, y, 0, 0);
		
		image = myImage;
		myComponent = component;
	}
	
	public Icon(Image myImage, int x, int y, int width, int height, Component component)
	{
		super(x, y, 0, 0);
		image = myImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		myComponent = component;
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(component);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}
	}
	
	public int getWidth()
	{
		return image.getWidth(null);
	}
	
	public int getHeight()
	{
		return image.getWidth(null);
	}
	
	public void setWidth(int width)
	{
		image = image.getScaledInstance(width, -1, Image.SCALE_SMOOTH);
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(myComponent);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}	
	}
	
	public void setHeight(int height)
	{
		image = image.getScaledInstance(-1, height, Image.SCALE_SMOOTH);
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(myComponent);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}	
	}
	
	public void setSize(int width, int height)
	{
		image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		
		//Wait for the image to load
		MediaTracker tracker = new MediaTracker(myComponent);
		tracker.addImage(image, 0);
		try{
			tracker.waitForAll();
		}
		catch(InterruptedException ex){
		}
	}
	
	@Override
	void draw(Graphics g) {
		g.drawImage(image, getX(), getY(), myComponent);
	}

}
